<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="10" :lg="10" :xl="10">
        <el-card class="box-card">
          <eHeader :param-bean="paramBean" :sup_this="sup_this" />
          <eForm ref="form" :sup_this="sup_this" />
          <!--表格渲染-->
          <DataGridTable
            v-loading="loading"
            ref="mainTable"
            :sup_this="sup_this"
            :data="data"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            :default-expand-all="expandAll"
            :show-page="false"
            :all-define-columns="['thirdCode','orgType']"
            grid-id="orgSetMngMainGridId"
            height="500px"
            row-key="id"
            @select-all="gridSelectAllChange"
            @selection-change="handleGridSelectionChange"
            @row-click="viewUser">
            <el-table-column label="名称" prop="orgName">
              <template slot="header" slot-scope="scope">
                <el-link @click="toggleTreeAllExpand">名称 <i class="el-icon-more el-icon--right" /> </el-link>
              </template>
              <template slot-scope="scope">
                <edit :data="scope.row" :sup_this="sup_this" />
              </template>
            </el-table-column>
            <DataGridColumn slot="thirdCode" label="编码" prop="thirdCode" />
            <DataGridColumnSlot slot="orgType" label="类别" prop="orgType" align="center">
              <template slot-scope="scope">
                <div v-for="item in dicts.org_type" :key="item.id">
                  <el-tag v-if="scope.row.orgType.toString() === item.dictCode" :type="scope.row.orgType ? '' : 'info'">{{ item.dictName }}</el-tag>
                </div>
              </template>
            </DataGridColumnSlot>
          </DataGridTable>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="14" :lg="14" :xl="14">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ orgName }} 用户</span>
            <div style="display: inline-block;margin: 0px 2px;">
              <el-button
                v-permission="['SYS_ADMIN','COMMON_ADMIN','ORGSET_ALL','ORGSET_CREATE']"
                :disabled="orgName == null || orgName === ''"
                class="filter-item"
                size="mini"
                type="primary"
                icon="el-icon-plus"
                @click="addUser">选择用户</el-button>
            </div>
            <userSelectList ref="userSelectList" @userSelectCallback="userSelectCallback" />
            <userOrgSet ref="userOrgSet" :sup_this="sup_this"/>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import eForm from './module/form'
import userOrgSet from './module/userOrgSet'
import initData from '@/mixins/initData'
import { loadDataUrl, saveUserOrgSet } from '@/api/system/orgSet'
import userSelectList from '@/views/system/user/module/userSelectList'

export default {
  components: { eHeader, edit, eForm, userOrgSet, userSelectList },
  mixins: [initData],
  // 设置数据字典
  dictTypes: ['org_type'],
  data() {
    return {
      orgInfo: {},
      orgName: '',
      delLoading: false, sup_this: this
    }
  },
  created() {
    this.url = loadDataUrl()
    this.expandAll = true
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.pageCount = false
      const tmpUserOrg = this.$refs.userOrgSet
      if (tmpUserOrg) {
        tmpUserOrg.data.rtnList = []
        tmpUserOrg.orgCode = ''
      }
      return true
    },
    afterInit(res) {
      this.orgName = ''
      const tmpUserOrg = this.$refs.userOrgSet
      tmpUserOrg.loadUserData()
    },
    viewUser(row, column, event) {
      if (row) {
        const tmpUserOrg = this.$refs.userOrgSet
        tmpUserOrg.orgCode = row.orgCode
        this.orgName = row.orgName
        this.orgInfo = row
        tmpUserOrg.loadUserData()
      }
    },
    userSelectCallback(selectedRows, userSelectObj) {
      const data = { userOrgSetVOList: [] }
      selectedRows.forEach(row => {
        const userOrgSetVo = {}
        userOrgSetVo.userId = row.id
        userOrgSetVo.userAccount = row.userAccount
        userOrgSetVo.cpyCode = this.orgInfo.cpyCode
        userOrgSetVo.orgCode = this.orgInfo.orgCode
        data.userOrgSetVOList.push(userOrgSetVo)
      })
      saveUserOrgSet(data).then(res => {
        userSelectObj.cancel()
        userSelectObj.loading = false
        this.$refs.userOrgSet.loadUserData()
      }).catch(err => {
        userSelectObj.loading = false
        console.log(err)
      })
    },
    addUser() {
      if (!this.orgName) {
        this.$notify({
          title: '请选择对应的组织机构',
          type: 'info',
          duration: 2000
        })
        return
      }
      const _this = this.$refs.userSelectList
      _this.init()
      _this.dialog = true
    }
  }
}
</script>

<style scoped>

</style>
